import 'package:flutter/material.dart';
import 'package:flutter_demo/chapter8-EventNotification/event-mechanism/HitTestBlocker.dart';
import 'package:flutter_demo/chapter8-EventNotification/gesture-conflict/CustomTapGestureRecongnizer.dart';

void main() {
  runApp(MaterialApp(
    home: Scaffold(
      appBar: AppBar(
        title: const Text("Notification Widget Demo"),
      ),
      body: CustomNotificationWidget(),
    ),
  ));
}

class CustomNotificationWidget extends StatefulWidget {
  @override
  _CustomNotificationWidgetState createState() =>
      _CustomNotificationWidgetState();
}

class _CustomNotificationWidgetState extends State<CustomNotificationWidget> {
  String _msg = '';

  @override
  Widget build(BuildContext context) {
    return NotificationListener<MyNotification>(
      onNotification: (notification) {
        setState(() => _msg += notification.msg + '  ');
        return true;
      },
      child: Center(
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            // 无法正常工作，NotificationListener监听的是子树，这里拿到的是根context
            ElevatedButton(
              onPressed: () => MyNotification('HI').dispatch(context),
              child: const Text('Send Notification1'),
            ),
            Builder(builder: (context) {
              return ElevatedButton(
                onPressed: () => MyNotification('HI').dispatch(context),
                child: const Text('Send Notification2'),
              );
            }),
            Text(_msg),
          ],
        ),
      ),
    );
  }
}

class MyNotification extends Notification {
  MyNotification(this.msg);

  final String msg;
}
